<template>
  <div class="register-container">
    <a-card class="register-card" title="注册">
      <a-form
        :model="formState"
        name="register"
        @finish="onFinish"
        @finishFailed="onFinishFailed"
      >
        <a-form-item
          name="username"
          :rules="[{ required: true, message: '请输入用户名!' }]"
        >
          <a-input v-model:value="formState.username" placeholder="用户名">
            <template #prefix>
              <user-outlined />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item
          name="password"
          :rules="[{ required: true, message: '请输入密码!' }]"
        >
          <a-input-password v-model:value="formState.password" placeholder="密码">
            <template #prefix>
              <lock-outlined />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item
          name="confirmPassword"
          :rules="[
            { required: true, message: '请确认密码!' },
            { validator: validatePassword }
          ]"
        >
          <a-input-password v-model:value="formState.confirmPassword" placeholder="确认密码">
            <template #prefix>
              <lock-outlined />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item
          name="nickname"
          :rules="[{ required: true, message: '请输入昵称!' }]"
        >
          <a-input v-model:value="formState.nickname" placeholder="昵称">
            <template #prefix>
              <smile-outlined />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            class="register-form-button"
            :loading="loading"
          >
            注册
          </a-button>
          已有账号? <router-link to="/">立即登录!</router-link>
        </a-form-item>
      </a-form>
    </a-card>
  </div>
</template>

<script>
import { defineComponent, reactive, ref, computed } from 'vue';
import { UserOutlined, LockOutlined, SmileOutlined } from '@ant-design/icons-vue';
import { message } from 'ant-design-vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '../stores/user';

export default defineComponent({
  components: {
    UserOutlined,
    LockOutlined,
    SmileOutlined,
  },
  setup() {
    const router = useRouter();
    const userStore = useUserStore();
    
    const formState = reactive({
      username: '',
      password: '',
      confirmPassword: '',
      nickname: '',
    });
    
    const validatePassword = async (rule, value) => {
      if (value !== formState.password) {
        return Promise.reject('两次输入的密码不一致!');
      }
      return Promise.resolve();
    };
    
    const onFinish = async (values) => {
      try {
        await userStore.register({
          username: values.username,
          password: values.password,
          nickname: values.nickname
        });
        
        message.success('注册成功，请登录');
        router.push('/');
      } catch (error) {
        // 错误已在store中处理
        console.error('注册失败:', error);
      }
    };
    
    const onFinishFailed = (errorInfo) => {
      console.log('Failed:', errorInfo);
    };
    
    return {
      formState,
      loading: computed(() => userStore.loading),
      validatePassword,
      onFinish,
      onFinishFailed,
    };
  },
});
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.register-card {
  width: 100%;
  max-width: 400px;
}

.register-form-button {
  width: 100%;
}

@media (max-width: 480px) {
  .register-card {
    max-width: 90%;
  }
}
</style>